﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using MediaLibraryManagement.BLL;
using MediaLibraryManagement.Common;
using MediaLibraryManagement.Entity;

namespace MediaLibraryManagement.UI
{
    public partial class Main : Form
    {
        #region Variables
        private User _currentUser;
        private AlbumSearchPane _albumSearchPane;
        private MovieSearchPane _movieSearchPane;
        private ArtistSearchPane _artistSearchPane;

        private List<Album> _albumsList;
        private List<Movie> _moviesList;
        private List<Artist> _artistsList;

        private Album _selectedAlbum;
        private Movie _selectedMovie;
        private Artist _selectedArtist;

        private string _viewMode = string.Empty;
        #endregion

        #region Constants
        private const string VIEW_MODE_MOVIE = "MOVIE";
        private const string VIEW_MODE_ALBUM = "ALBUM";
        private const string VIEW_MODE_ARTIST = "ARTIST";
        #endregion

        public User CurrentUser
        {
            get { return _currentUser; }
            set
            {
                _currentUser = value;
                if(_currentUser != null)
                {
                    string fullName = string.Format("{0} {1}", _currentUser.FirstName, _currentUser.LastName).Trim();
                    tssbtnLoggedInUser.Text = string.Format("{0} ({1})", fullName, _currentUser.UserName).Trim();
                }
                else
                {
                    tscboGuest.Text = "Khách";
                }
            }
        }

        public Main()
        {
            InitializeComponent();
            tsMainMenu.Visible = false;
            tsmiAddAlbum.Enabled = false;
            tsbManage.Visible = false;
            tssbtnLoggedInUser.Visible = false;
        }

        #region Private Methods

        private void AddAlbumsToListView(List<Album> albums)
        {
            lvwItems.Columns.Add("", 80, HorizontalAlignment.Center);
            lvwItems.Columns.Add("Tên album", 120);
            lvwItems.Columns.Add("Thể loại");
            lvwItems.Columns.Add("Đánh giá");

            foreach (Album album in albums)
            {
                ListViewItem item = new ListViewItem();
                item.ImageIndex = imlstThumbnails.Images.Count;
                item.SubItems.Add((album.Title).Trim());
                item.SubItems.Add(album.Genre);
                item.SubItems.Add(Math.Round(album.Rate, 2) + "/5");

                imlstThumbnails.Images.Add(album.Image);
                lvwItems.Items.Add(item);
            }
        }

        private void AddMoviesToListView(List<Movie> movies)
        {
            lvwItems.Columns.Add("", 80, HorizontalAlignment.Center);
            lvwItems.Columns.Add("Tên phim", 120);
            lvwItems.Columns.Add("Thể loại");
            lvwItems.Columns.Add("Đánh giá");

            foreach (Movie movie in movies)
            {
                ListViewItem item = new ListViewItem();
                item.ImageIndex = imlstThumbnails.Images.Count;
                item.SubItems.Add((movie.Title).Trim());
                item.SubItems.Add(movie.Genre);
                item.SubItems.Add(Math.Round(movie.Rate, 2) + "/10");

                imlstThumbnails.Images.Add(movie.Image);
                lvwItems.Items.Add(item);
            }
        }

        private void AddArtistsToListView(List<Artist> artists)
        {
            lvwItems.Columns.Add("", 80, HorizontalAlignment.Center);
            lvwItems.Columns.Add("Tên nghệ sĩ", 120);
            lvwItems.Columns.Add("Quốc gia");

            foreach (Artist artist in artists)
            {
                ListViewItem item = new ListViewItem();
                item.ImageIndex = imlstThumbnails.Images.Count;
                item.SubItems.Add((artist.FirstName + " " + artist.LastName).Trim());
                item.SubItems.Add(artist.Country);

                imlstThumbnails.Images.Add(artist.Image);
                lvwItems.Items.Add(item);
            }
        }

        #endregion

        #region Event Handlers

        private void tsmiLogin_Click(object sender, EventArgs e)
        {
            Login frmLogin = new Login();
            if (frmLogin.ShowDialog() == DialogResult.OK)
            {
                this.CurrentUser = GlobalObjects.CurrentUser;
                tscboGuest.Visible = false;
                tssbtnLoggedInUser.Visible = true;
                tsMainMenu.Visible = true;
                tsbAdd.Visible = false;
                tsbManage.Visible = false;
                switch(this.CurrentUser.Privilege)
                {
                    case Privileges.Administrator:
                        tsbAdd.Visible = true;
                        tsbManage.Visible = true;
                        break;
                    case Privileges.Contributor:
                        tsbAdd.Visible = true;
                        break;
                    case Privileges.Viewer:
                        break;
                    default: break;
                }
            }
        }

        private void tsmiLogout_Click(object sender, EventArgs e)
        {
            string message = "Bạn có chắc muốn đăng xuất?";
            if (MessageBox.Show(message, "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.None) == DialogResult.Yes)
            {
                GlobalObjects.CurrentUser = null;
                this.CurrentUser = null;
                tscboGuest.Visible = true;
                tssbtnLoggedInUser.Visible = false;
                tsMainMenu.Visible = false;
            }
        }

        private void tsmiRegister_Click(object sender, EventArgs e)
        {
            UserDetails frmRegistration = new UserDetails();
            frmRegistration.ShowDialog();
        }

        private void tsmiManageAccount_Click(object sender, EventArgs e)
        {
            UserDetails frmUserDetails = new UserDetails();
            frmUserDetails.ShowDialog();
        }

        private void tsmiAddAlbum_Click(object sender, EventArgs e)
        {
            AlbumDetails frmAlbumDetails = new AlbumDetails(_selectedArtist, null);
            frmAlbumDetails.ShowDialog();
        }

        private void tsmiAddMovie_Click(object sender, EventArgs e)
        {
            MovieDetails frmMovieDetails = new MovieDetails();
            frmMovieDetails.ShowDialog();
        }

        private void tsmiAddArtist_Click(object sender, EventArgs e)
        {
            ArtistDetails frmArtistDetails = new ArtistDetails();
            frmArtistDetails.ShowDialog();
        }

        private void tssbtnLoggedInUser_ButtonClick(object sender, EventArgs e)
        {
            UserDetails frmUserDetails = new UserDetails();
            frmUserDetails.ShowDialog();
        }

        private void tsmiManageUsers_Click(object sender, EventArgs e)
        {
            UserManagement frmUserManagement = new UserManagement();
            frmUserManagement.ShowDialog();
        }

        private void cboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlSearchLowerPane.Controls.Clear();
            _albumSearchPane = null;
            _movieSearchPane = null;
            _artistSearchPane = null;
            switch (cboSearchType.SelectedIndex)
            {
                case 0:
                    _viewMode = VIEW_MODE_ALBUM;
                    _albumSearchPane = new AlbumSearchPane();
                    pnlSearchLowerPane.Controls.Add(_albumSearchPane);
                    break;
                case 1:
                    _viewMode = VIEW_MODE_MOVIE;
                    _movieSearchPane = new MovieSearchPane();
                    pnlSearchLowerPane.Controls.Add(_movieSearchPane);
                    break;
                case 2:
                    _viewMode = VIEW_MODE_ARTIST;
                    _artistSearchPane = new ArtistSearchPane();
                    pnlSearchLowerPane.Controls.Add(_artistSearchPane);
                    break;
                default:
                    break;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            // Clear toàn bộ kết quả search trên list view trước đó
            imlstThumbnails.Images.Clear();
            lvwItems.Columns.Clear();
            lvwItems.Items.Clear();

            // Xóa toàn bộ biến toàn cục chứa kết quả search trước đó
            _albumsList = null;
            _moviesList = null;
            _artistsList = null;
            switch (cboSearchType.SelectedIndex)
            {
                case 0:
                    AlbumBLL albumBLL = new AlbumBLL();
                    _albumsList = albumBLL.Search(_albumSearchPane.Title, _albumSearchPane.Genre, _albumSearchPane.ArtistName, _albumSearchPane.ReleaseYear);
                    AddAlbumsToListView(_albumsList);
                    lvwItems.SmallImageList = imlstThumbnails;
                    break;
                case 1:
                    MovieBLL movieBLL = new MovieBLL();
                    _moviesList = movieBLL.Search(_movieSearchPane.Title, _movieSearchPane.Genre, _movieSearchPane.ReleaseYear);
                    AddMoviesToListView(_moviesList);
                    lvwItems.SmallImageList = imlstThumbnails;
                    break;
                case 2:
                    ArtistBLL artistBLL = new ArtistBLL();
                    _artistsList = artistBLL.Search(_artistSearchPane.ArtistName, _artistSearchPane.Company, _artistSearchPane.Country);
                    AddArtistsToListView(_artistsList);
                    lvwItems.SmallImageList = imlstThumbnails;
                    break;
                default:
                    break;
            }
        }

        private void lvwItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlInfo.Controls.Clear();
            _selectedAlbum = null;
            _selectedMovie = null;
            _selectedArtist = null;
            if (lvwItems.SelectedItems.Count > 0)
            {
                switch (_viewMode)
                {
                    case VIEW_MODE_ALBUM:
                        _selectedAlbum = _albumsList[lvwItems.SelectedIndices[0]];
                        AlbumInfoPane ucAlbumInfoPane = new AlbumInfoPane();
                        ucAlbumInfoPane.Album = _selectedAlbum;
                        pnlInfo.Controls.Add(ucAlbumInfoPane);
                        break;
                    case VIEW_MODE_MOVIE:
                        _selectedMovie = _moviesList[lvwItems.SelectedIndices[0]];
                        MovieInfoPane ucMovieInfoPane = new MovieInfoPane();
                        ucMovieInfoPane.Movie = _selectedMovie;
                        pnlInfo.Controls.Add(ucMovieInfoPane);
                        break;
                    case VIEW_MODE_ARTIST:
                        _selectedArtist = _artistsList[lvwItems.SelectedIndices[0]];
                        ArtistInfoPane ucArtistInfoPane = new ArtistInfoPane();
                        ucArtistInfoPane.Artist = _selectedArtist;
                        pnlInfo.Controls.Add(ucArtistInfoPane);
                        break;
                    default: break;
                }
            }
            if (_selectedArtist != null)
            {
                tsmiAddAlbum.Enabled = true;
            }
            else
            {
                tsmiAddAlbum.Enabled = false;
            }
        }

        #endregion
    }
}
